package com.hz.ruoyilgorder.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.hz.rouyigonggong.domain.LgInboundOrder;
import com.hz.rouyigonggong.domain.LgSellerInfo;

import com.hz.rouyigonggong.domain.basedomain.BaseDomain;
import com.hz.rouyigonggong.domain.dto.LgInboundOrderDTO;
import com.hz.ruoyilgorder.api.service.LgSellerInfoService;
import com.hz.ruoyilgorder.config.LgInboundOrderConverter;

import com.hz.ruoyilgorder.service.LgInboundOrderService;
import com.ruoyi.common.core.constant.HttpStatus;
import com.ruoyi.common.core.utils.PageUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
//import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @version 1.0
 * @Author yzw
 * @Date 2025/3/7 17:00
 * @注释 入库订单
 */

@RestController
@RequestMapping("/inorder")
public class LgInboundOrderController {

    @Autowired
    private LgInboundOrderService lgInboundOrderService;
    @Autowired
    private LgSellerInfoService lgSellerInfoService;


    /**
    * @Description: 通过单个[id] 查找商品具体信息
    * @Param: [id]
    * @return: com.ruoyi.common.core.web.domain.AjaxResult
    * @Author: grx
    * @Date: 19:30 2025/3/15
    */
    @GetMapping(value = "/getSellInfoOrderById")
    public AjaxResult getSellInfoOrderById(Integer id) {

        return AjaxResult.success(lgInboundOrderService.getSellInfoOrderById(id));
    }
    /**
    * @Description: 通过[sellerId] 查找入库商品列表
    * @Param: [sellerId]
    * @return: com.ruoyi.common.core.web.domain.AjaxResult
    * @Author: grx
    * @Date: 19:29 2025/3/15
    */
    @GetMapping(value = "/getOrderBySellerId")
    public AjaxResult getOrderBySellerId(Integer sellerId) {

        return AjaxResult.success(lgInboundOrderService.getOrderBySellerId(sellerId));
    }

    //查询 入库订单
    @RequiresPermissions("inorder:every:list")
    @GetMapping("/getList")
    public TableDataInfo getList(LgInboundOrderDTO entity){
        startPage();
        entity.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        List<LgInboundOrderDTO> list = lgInboundOrderService.querylist(entity);
        return  getDataTable(list);
    }
//根据id查询
    @RequiresPermissions("inorder:every:query")
    @GetMapping(value = "/getInfo/{orderNumber}")
    public AjaxResult getInfo(@PathVariable Integer orderNumber) {

        return AjaxResult.success(lgInboundOrderService.getInfo(orderNumber));
    }

    @RequiresPermissions("inorder:every:edit")
    @PostMapping("/edit")
    @Transactional
//    @GlobalTransactional//分布式事务
    public AjaxResult edit(@Validated @RequestBody LgInboundOrderDTO order) {
        order.setCreateBy(String.valueOf(BaseDomain.getUserId()));
        LgInboundOrder entity = LgInboundOrderConverter.toEntity(order);
        //当入库完成时，插入卖家仓储信息表
        if(entity.getStatus()==4){
            System.out.println(11111111);
            LgSellerInfo lgSellerInfo = new LgSellerInfo();
            lgSellerInfo.setInboundOrderNumber(entity.getOrderNumber());
            lgSellerInfo.setRemainingQuantity(entity.getActualQuantity());
            lgSellerInfo.setRemainingWeight(entity.getVerifiedWeight().intValue());
//            lgSellerInfoService.insertSellerInfo(lgSellerInfo);
            System.out.println(lgSellerInfoService.insertSellerInfo(lgSellerInfo));
        }
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("inbound_id",entity.getInboundId());
        return AjaxResult.success(lgInboundOrderService.update(order, updateWrapper));
    }



    @RequiresPermissions("inorder:every:remove")
    @GetMapping("/remove/{inboundIds}")
    public AjaxResult remove(@PathVariable Long[] inboundIds) {


        return AjaxResult.success(lgInboundOrderService.removeByIds(Arrays.asList(inboundIds)));
    }






    protected void startPage()
    {
        PageUtils.startPage();
    }
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setRows(list);
        rspData.setMsg("查询成功");
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }




}
